package org.clever.sharding.jdbc.balance;

import io.shardingsphere.api.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithm;
import io.shardingsphere.api.algorithm.masterslave.RoundRobinMasterSlaveLoadBalanceAlgorithm;
import org.clever.sharding.jdbc.aop.ShardingHolder;

import java.util.List;

/**
 * 选择读库调度类
 */
public class ShardingLoadBalance implements MasterSlaveLoadBalanceAlgorithm {

    private static final RoundRobinMasterSlaveLoadBalanceAlgorithm roundRobinMasterSlaveLoadBalanceAlgorithm = new RoundRobinMasterSlaveLoadBalanceAlgorithm();

    @Override
    public String getDataSource(String name, String masterDataSourceName, List<String> slaveDataSourceNames) {
        if (ShardingHolder.getSharding()) {
            return roundRobinMasterSlaveLoadBalanceAlgorithm.getDataSource(name, masterDataSourceName, slaveDataSourceNames);
        }
        return masterDataSourceName;
    }
}
